import 'dart:async';

import 'package:get/get.dart';
import 'package:tencent_kit/tencent_kit.dart';
import 'package:zrjz/comm/utils/log_utils.dart';

typedef QQInfoCallback = void Function(String openId);

class QQPlatformService extends GetxService {
  static QQPlatformService get to => Get.find();

  final String _kTencentAppID = '12345678';
  final String _kUniversalLink = 'https://xxxxxxxx/'; // iOS 请配置

  StreamSubscription<TencentResp>? _respSubs; //标记是否已经初始化
  // TencentLoginResp? _authResp;

  //回调详情数据
  QQInfoCallback? mQQInfoCallback;

  /// 服务初始化的时候，构造创建
  Future<void> _initQQ() async {
    Log.d('初始化QQ平台SDK');

    _respSubs = TencentKitPlatform.instance.respStream().listen(_listenResp);

    await TencentKitPlatform.instance.setIsPermissionGranted(granted: true);
    Log.d('初始化QQ平台 - 已授权获取设备信息/同意隐私协议');

    //注册App
    Log.d('初始化QQ平台 - 注册QQ平台的AppId');
    await TencentKitPlatform.instance.registerApp(
      appId: _kTencentAppID,
      universalLink: _kUniversalLink,
    );
  }

  /// 用户授权登录
  Future<void> loginWithQQ(QQInfoCallback callback) async {
    mQQInfoCallback = callback;

    if (_respSubs == null) {
      await _initQQ();
    }

    TencentKitPlatform.instance.login(
      scope: <String>[TencentScope.kGetSimpleUserInfo],
    );
  }

  /// 分享到微信
  Future<void> shareWithQQ(String message) async {
    if (_respSubs == null) {
      await _initQQ();
    }

    TencentKitPlatform.instance.shareText(
      scene: TencentScene.kScene_QQ,
      summary: message,
    );
  }

  /// 是否安装QQ
  Future<bool> isQQInstalled() async {
    if (_respSubs == null) {
      await _initQQ();
    }

    return TencentKitPlatform.instance.isQQInstalled();
  }

  /// 是否安装QQ
  Future<bool> isTIMInstalled() async {
    if (_respSubs == null) {
      await _initQQ();
    }

    return TencentKitPlatform.instance.isTIMInstalled();
  }

  ///初始化的时候添加的监听，当授权登录成功之后获取当前用户的详细信息
  void _listenResp(TencentResp resp) {
    if (resp is TencentLoginResp) {
      // _authResp = resp;
      if (resp.openid?.isNotEmpty == true && resp.accessToken?.isNotEmpty == true) {
        Log.d('登录成功,openid: ${resp.openid} - accessToken: ${resp.accessToken}');
        //获取QQ用户的详情
        mQQInfoCallback?.call(resp.openid!);
      } else {
        Log.d('登录失败');
      }
    } else if (resp is TencentShareMsgResp) {
      final String content = 'share: ${resp.ret} - ${resp.msg}';
      Log.d('分享 $content');
    }
  }

  // void _fetchQQInfo() async {
  //   if ((_authResp?.isSuccessful ?? false) && !(_authResp!.isExpired ?? true)) {
  //     final TencentUserInfoResp userInfo = await TencentApi.getUserInfo(
  //       appId: _kTencentAppID,
  //       openid: _authResp!.openid!,
  //       accessToken: _authResp!.accessToken!,
  //     );
  //
  //     if (userInfo.isSuccessful) {
  //       mQQInfoCallback?.call(userInfo, _authResp!.openid!);
  //
  //       Log.d("获取Info结果： ${userInfo.toJson()}");
  //     } else {
  //       mQQInfoCallback?.call(null, _authResp!.openid!);
  //       Log.d("获取Info失败了");
  //     }
  //   }
  // }
}
